import 'package:flutter/foundation.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../../repository/dto/banner_dto.dart';
import '../../repository/dto/list_dto.dart';
import '../../repository/api.dart';

class HomeViewModel with ChangeNotifier {
  int pageCount = 0;
  List<BannerItemData?>? bannerList;
  List<ListItemData?>? listData = [];


  Future getBanner() async {
    bannerList = await Api.instance.getBanner() ?? [];
    notifyListeners();
  }

  Future getAllList([bool? loadMore = false]) async {
    if(loadMore == true) {
      pageCount++;
    } else {
      pageCount = 0;
      await getTopList();
    }
    await getList();
  }

  Future getList() async {
    List<ListItemData?>? list = await Api.instance.getList('$pageCount');
    listData?.addAll(list ?? []);
    notifyListeners();
  }

  Future getTopList() async {
    List<ListItemData?>? list = await Api.instance.getTopList();
    listData?.clear();
    listData?.addAll(list ?? []);
    notifyListeners();
  }

  tagCollect(int index) {
    listData![index]!.collect = !listData![index]!.collect;
    notifyListeners();
  }

  Future<bool?> collect(int id, int index) async {
    await Api.instance.collect(id);
    tagCollect(index);
    Fluttertoast.showToast(msg: '收藏成功');
    return true;
  }

  Future<bool?> unCollect(int id, int index) async {
    await Api.instance.unCollect(id);
    tagCollect(index);
    Fluttertoast.showToast(msg: '取消收藏成功');
    return true;
  }

}